/*
 * Copyright (c) 2011 Sapienza University of Rome.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the
 *   distribution.
 * - Neither the name of the Sapienza University of Rome nor the names of
 *   its contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL SAPIENZA
 * UNIVERSITY OF ROME OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * @author Ugo Colesanti <colesanti@dis.uniroma1.it>
 * @version 1.01 (October 10, 2011)
 *
 */

configuration ManagerAppC {

	provides interface StdControl ;
	provides interface Send ;
	provides interface Receive ;
	provides interface CollectionPacket ;
	provides interface UnicastNameFreeRouting ;
	provides interface CtpPacket ;
	provides interface ScheduleManagement ;
	provides interface DISSenseSampling ;
	provides interface DISSenseStatistics ;

}
implementation {

	// DISSense's components
	components ManagerC ;
	components LplManagerC ;


	components AdaptiveEngineC ;

	// DISSense's Manager timers
	components new TimerMilliC() as GeneralTimer ;
	components new TimerMilliC() as ProtocolTimer ;
	components new TimerMilliC() as ShowResyncTimer ;
	components new TimerMilliC() as SyncWaitTimer ;
	components new TimerMilliC() as DISSenseSamplingTimer ;

	// CTP components
	components CollectionC as Collector ;
	components new CollectionSenderC(AM_DATA_MSG) ;

	// Leds
#ifdef NOLEDS
	components NoLedsC as LedsComp ;
#else
	components LedsC as LedsComp ;
#endif

// Random component for drift in tossim
#ifdef TOSSIM
	components RandomC ;
	ManagerC.Random -> RandomC ;
#endif


// Lpl is skipped if not using tossim (not supported)
#ifndef TOSSIM
	components CC2420ActiveMessageC as LplRadio;
	ManagerC.LowPowerListening -> LplRadio ;
#endif


	// DISSense interfaces
	StdControl = ManagerC ;
	Send = ManagerC ;
	Receive = ManagerC ;
	ScheduleManagement = ManagerC ;
	DISSenseSampling = ManagerC ;
	DISSenseStatistics = ManagerC ;

	// CTP interfaces
	CtpPacket = Collector ;
	CollectionPacket = Collector ;
	UnicastNameFreeRouting = Collector ;

	// DISSense's Manager timers
	ManagerC.GeneralTimer -> GeneralTimer ;
	ManagerC.ProtocolTimer -> ProtocolTimer ;
	ManagerC.ShowResyncTimer -> ShowResyncTimer ;
	ManagerC.SyncWaitTimer -> SyncWaitTimer ;
	ManagerC.DISSenseSamplingTimer -> DISSenseSamplingTimer ;

	// DISSense Reboot linking
	ManagerC.Reboot -> LplManagerC ;



	/* ------------------------------------------------------------
	 * 					LPLManager Linking
	 * ------------------------------------------------------------
	 */

	// DISSense Manager interfaces: to be described
	LplManagerC.ProtocolScheduler -> ManagerC ;
	LplManagerC.ScheduleManagement -> ManagerC ;


	// Send / Receive interface with Lpl Management
//	components new AMSenderC(AM_LPL_SYNC_MSG) as LplSyncSender;
	components new AMReceiverC(AM_LPL_SYNC_MSG) as LplSyncReceiver;
	components new DirectAMSenderC(AM_LPL_SYNC_MSG) as LplSyncSender ;

	LplManagerC.LplSyncSend -> LplSyncSender ;
	LplManagerC.LplSyncReceive -> LplSyncReceiver ;
#ifndef TOSSIM
	LplManagerC.LowPowerListening -> LplRadio ;
#endif

	// Timers for trickle-like resync requests
	components new TimerMilliC() as LPLSyncMngmtTimer ;
	components new TimerMilliC() as TryResyncTimer ;
	components new TimerMilliC() as LPLResetBroadcastTimer ;
	components new TimerMilliC() as WatchDogTimer ;
	LplManagerC.TryResyncTimer -> TryResyncTimer ;
	LplManagerC.LPLSyncMngmtTimer -> LPLSyncMngmtTimer ;
	LplManagerC.LPLResetBroadcastTimer -> LPLResetBroadcastTimer ;
	LplManagerC.WatchDogTimer -> WatchDogTimer ;

	// Random component for trickle-like resync timer management
	components RandomC as LplRandom ;
	LplManagerC.Random -> LplRandom ;

	LplManagerC.Leds -> LedsComp ;


	/* ------------------------------------------------------------
	 * 					NTPManager Linking
	 * ------------------------------------------------------------
	 */

	// Send / Receive interface over serial port
#ifdef SINK
	components NtpManagerC as NtpC ;
	components new SerialAMSenderC(AM_SINKGATEWAYMSG) as NtpStatusSenderC ;
	components new SerialAMReceiverC(AM_SINKGATEWAYMSG) as NtpStatusReceiverC ;
	components new SerialAMSenderC(AM_HEARTBEATMSG) as NtpHTSenderC ;
	components new SerialAMReceiverC(AM_HEARTBEATMSG) as NtpHTReceiverC ;
	NtpC.AMSend -> NtpStatusSenderC.AMSend ;
	NtpC.Receive -> NtpStatusReceiverC.Receive ;
	NtpC.HTSend -> NtpHTSenderC.AMSend ;
	NtpC.HTReceive -> NtpHTReceiverC.Receive ;


	// Timer for HeartBeat
	components new TimerMilliC() as NtpTimer ;
	NtpC.Timer -> NtpTimer ;

	// Leds (to be removed)
	NtpC.Leds -> LedsComp ;

	// DISSense interface: to be described
	NtpC.ScheduleManagement -> ManagerC ;
	NtpC.Reset -> ManagerC ;
#else
	components DummyNtpManagerC as NtpC ;
#endif

	/* ------------------------------------------------------------
	 * 		Linking between Manager and DISSense's components
	 * ------------------------------------------------------------
	 */
	ManagerC.LplResync -> LplManagerC ;
	ManagerC.AdaptiveLayer -> AdaptiveEngineC ;
	ManagerC.NtpSynchronization -> NtpC ;


	/* ------------------------------------------------------------
	 * 		Linking between Manager and CTP (orig. interfaces)
	 * ------------------------------------------------------------
	 */
	ManagerC.RoutingControl -> Collector ;
	ManagerC.RootControl -> Collector ;
	ManagerC.CtpInfo -> Collector ;
	ManagerC.CollectionPacket -> Collector ;
	ManagerC.SubSend -> CollectionSenderC ;
	ManagerC.SubReceive -> Collector.Receive[AM_DATA_MSG] ;


	/* ------------------------------------------------------------
	 * 		Linking between Manager and CTP (new interfaces)
	 * ------------------------------------------------------------
	 */
	ManagerC.RMSynchronization -> Collector ;
	ManagerC.RMControl -> Collector ;


	// Debug leds
	ManagerC.Leds -> LedsComp ;

}
